<?php

namespace App\Service\Collect;


use App\Service\BaiduNlp;
use QL\QueryList;
use GuzzleHttp\Client;

class Eastmoney extends Base
{
    // 从东方财富采集
    //   采集接口用curl不成功，
    //     QueryList与GuzzleHttp也不成功
    //   只有file_get_contents执行成功

    // 初始化
    protected function init()
    {
        parent::init();
    }

    // 获取api接口的列表(正式)
    public function getApiList($keyword, $type = 'ann', $page = 1, $size = 10)
    {
        if(empty($keyword)){
            return $this->return_die('传递采集关键字！');
        }
        if(!in_array($type, ['tiezi', 'news', 'carticle', 'ann'])){
            return $this->return_die('传递正确采集类型！');
        }

        try{
            $time  = time();
            $url   = 'https://search-api-web.eastmoney.com/search/jsonp';
            $cb    = 'jQuery35108425565408928009_'.$time;
            $param = $this->makeApiParam($keyword, $type, $page, $size);
            $post  = ['cb' => $cb, 'param' => json_encode($param), '_' => $time];
            $url   = $url.'?'.http_build_query($post);
            $res   = file_get_contents($url);  // 执行成功
            $json  = substr($res, strlen($cb) + 1, -1);
            $array = json_decode($json, true);
        } catch(\Exception $e){
            return $this->return_false($e->getMessage());
        }

        if($array['code'] != 0){
            return $this->return_die('请求返回错误！');
        }

        $type = $param['type'][0];

        return $array['result'][$type];
    }


    // 获取api接口的列表(手机端)
    public function getApiWapList($url, $page = 1, $size = 20)
    {
        try{
            $post  = [
                'param'   => 'code=600624&p='.$page.'&ps='.$size.'&sorttype=1',
                'plat'    => 'wap',
                'version' => '200',
                'path'    => '/webarticlelist/api/Article/WebArticleList',
                'env'     => 1,
            ];
            $json  = $this->apiCurl()->post($post)->url($url)->request();  // ->header($header)->returnHeader(true)
            $array = json_decode($json, true);
        } catch(\Exception $e){
            return $this->return_false($e->getMessage());
        }

        if(empty($array['re'])){
            return $this->return_die('请求返回错误！');
        }

        return $array['re'];
    }

    // 获取页面接口的列表(正式)
    public function getHtmlList($url)
    {
        if(empty($url)){
            return $this->return_die('传递采集关键字！');
        }

        try{
            //$header = $this->getHeader($domain);
            $returnHtml   = $this->apiCurl()->url($url)->request();  // ->header($header)->returnHeader(true)
            $start_string = '<script>var article_list=';
            $start_len    = strlen($start_string);
            $start_pos    = strrpos($returnHtml, $start_string);
            $end_pos      = strrpos($returnHtml, ';    var other_list=');
            $jsonString   = substr($returnHtml, $start_pos + $start_len, $end_pos - $start_pos - $start_len);
            $jsonData     = json_decode($jsonString, true);
        } catch(\Exception $e){
            var_dump($e->getMessage());

            return $this->return_false($e->getMessage());
        }


        var_export($jsonData);

        die;
        /*if($array['code'] != 0){
            return $this->return_die('请求返回错误！');
        }

        $type = $param['type'][0];*/

        //return $array['result'][$type];
    }

    // 保存到数据表
    public function saveApiDataToDb($_item, $key, $type, $is_file = false)
    {
        // 执行采集
        $_title   = $this->removeHtml(trim($_item['title']));
        $_href    = trim($_item['url']);
        $_content = $this->removeHtml(trim($_item['content']));
        $_praise  = 0;
        $_cover   = '';
        $_comment = 0;
        if($type == 'tiezi'){
            $_id    = $_item['id'];  // 文章ID
            $_stock = $_item['innerCode']; // 股票代码
            $_date  = strtotime($_item['createTime']);
        } elseif($type == 'news'){
            $_date   = strtotime($_item['date']);
            $_author = $_item['mediaName'];
            $_code   = $_item['code'];
        } elseif($type == 'carticle'){
            $_id     = $_item['id'];  // 文章ID
            $_code   = $_item['code'];
            $_postId = $_item['postId'];
            $_author = $_item['nickname'];
            $_cover  = $_item['portrait'];
            $_date   = strtotime($_item['date']);
        } elseif($type == 'ann'){
            $_columnCode = $_item['columnCode'];
            $_code       = $_item['code'];
            $_date       = strtotime($_item['date']);
            $_author     = $_item['securityFullName'];
            $_author     = '';
            // 公告的一些特殊处理
            $_content = $_content.'...';
            $_keyword = str_replace('股份', '', $this->keyword);
            $_keyword = str_replace('*', '', $_keyword);
            //$_keyword = substr($keyword, 0, strpos($keyword, '股份'));
            // $_title 包含 【..易见..】
            if((!preg_match('/【.*?'.$_keyword.'.*?】/', $_title)) && (!preg_match('/.*?'.$_keyword.'.*?/', $_item['securityFullName']))){
                return $this->return_false(' '.($key + 1).'：==>公告标题不包含关键字:'.$_keyword.' 采集公告：'.$_title);
            }
        }

        // 判断链接是否已经采集
        if($is_file){
            $info = $this->isGatherFile($_href);
        } else{
            $info = $this->isGather($_href);
        }
        if($info){
            if(empty($info['sentiment'])){
                $sentiment = $this->baiduNlp($info['text'] ?: $info['description']);
                if($sentiment){
                    if($is_file){
                        $info['sentiment'] = $sentiment;
                        $this->updateFile($_href, $info);
                    } else{
                        $this->updateArticle($info['id'], ['sentiment' => $sentiment]);
                    }
                }
            }

            return $this->return_false(' '.($key + 1).'：==> 已经采集文章：'.$_title);
        }

        // 只采集今天的
        if($_date < $this->startTime){
            return $this->return_die(' '.($key + 1).'：==> 文章：不是今天的文章：'.$_title);
        }

        // 如果没有标题
        /*if(empty($_title)){
            return $this->return_false(' '.($key + 1).'：==> 文章：没有标题，不能采集：'.$_title);
        }*/

        // 查询
        //$this->showMsg(' '.($key + 1).'：==> 采集文章：'.$_title);
        if($type == 'tiezi'){
            try{
                $_ql = QueryList::get($_href);
            } catch(\Exception $e){
                return $this->return_false($e->getMessage());
            }
        }


        //  主要获取 点赞，评论，用户，标题，内容
        if($type == 'tiezi'){
            $_title   = $_ql->find('#middle #left #newscontent  .newstitle')->text();
            $_author  = $_ql->find('#middle #left #newscontent  .newsauthor .name')->text();
            $_praise  = $_ql->find('#middle #left #newscontent  .newsinter .inter_left .likebox')->text();
            $_comment = $_ql->find('#middle #left #newscontent  .newsinter .inter_left .replybox')->text();
            $_time    = $_ql->find('#middle #left #newscontent  .newsauthor .time')->text();
            $_date    = strtotime($_time);
        } elseif($type == 'news'){
        } elseif($type == 'carticle'){
        } elseif($type == 'ann'){
        }

        // 全匹配 精准匹配
        if((strpos($_content, $this->keyword) === false) && strpos($_title, $this->keyword) === false){
            return $this->return_false('==> 文章：文章没有精准匹配到搜索的关键字！');
        }
        // 排除关键字 宋正兴
        if(($this->keyword == '董事长宋正') && (strpos($_content, '宋正兴') !== false) || strpos($_title, '宋正兴') !== false){
            return $this->return_false('==> 文章：文章匹配到其他关键词！');
        }

        // 判断 情感倾向
        //  "sentiment":2,   // 表示情感极性分类结果，0:负向，1:中性，2:正向
        /*$sentiment = '';  // 默认
        $full      = [0 => '负向', 1 => '中性', 2 => '正向'];
        $result    = (new BaiduNlp())->sentiment($_content);
        if(isset($result['items'][0]['sentiment'])){
            $sentiment = $full[$result['items'][0]['sentiment']] ?? $sentiment;
        }*/
        $sentiment = $this->baiduNlp($_content);


        // 来源
        if($type == 'ann'){
            $site_id   = 6;
            $from_site = '东风财富网公告';
            $site_id   = 13;
            $from_site = '公告';
            $_channel  = '公告';
            $channel   = 'notice';
        } elseif($type == 'tiezi'){
            $site_id   = 3;
            $from_site = '东风财富网股吧';
            $_channel  = '股吧';
            $channel   = 'thread';
        } elseif($type == 'news'){
            $site_id   = 4;
            $from_site = '东风财富网资讯';
            $_channel  = '资讯';
            $channel   = 'article';
        } elseif($type == 'carticle'){
            $site_id   = 5;
            $from_site = '东风财富网文章';
            $_channel  = '文章';
            $channel   = 'article';
        }

        $_row = [
            'channel'      => $channel,
            'sentiment'    => $sentiment,
            'link'         => $_href,
            'title'        => $_title,
            'description'  => $_content,
            'text'         => $_content,
            'time'         => $_date,
            'author'       => trim($_author),
            'from_channel' => $_channel,
            'from_site'    => $from_site,
            'cover'        => $_cover,
            'site_id'      => $site_id,
            'praise_no'    => intval($_praise),
            'comment_no'   => intval($_comment),
        ];

        //var_dump($_row);
        if($is_file){
            $res = $this->saveFile($_row);
        } else{
            $res = $this->saveArticle($_row);
        }

        return $res;
    }

    // 获取api接口的列表(测试)
    //   采集接口用curl不成功，
    //     QueryList与GuzzleHttp也不成功
    //   只有file_get_contents执行成功
    public function _getApiList($keyword, $type = 'ann', $page = 1, $size = 10)
    {
        if(empty($keyword)){
            return $this->return_false('传递采集关键字！');
        }
        if(!in_array($type, ['tiezi', 'news', 'carticle', 'ann'])){
            return $this->return_false('传递正确采集类型！');
        }

        try{
            $time  = time();
            $url   = 'https://search-api-web.eastmoney.com/search/jsonp';
            $cb    = 'jQuery35108425565408928009_'.$time;
            $param = $this->makeApiParam($keyword, $type, $page, $size);
            $post  = ['cb' => $cb, 'param' => json_encode($param), '_' => $time];

            // 使用 curl 获取不到数据
            $header = $this->getHeader();
            $res    = $this->apiCurl()->header($header)->get($post)->request($url);
        } catch(\Exception $e){
            return $this->return_false($e->getMessage());
        }


        //$url = $url.'?'.http_build_query($post);
        $url = $url.'?'.http_build_query($post);
        //$res = file_get_contents($url);  // 执行成功
        var_dump($res);

    }


    // header参数（cookie是必传的）
    protected function getHeader($cookie = '')
    {
        return [
            'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
            'Accept-Encoding:deflate, br',
            'Accept-Language:zh-CN,zh;q=0.9',
            'Cache-Control:max-age=0',
            'Connection:keep-alive',
            'Cookie:qgqp_b_id=0077acc4c61d68ecf25a76ffca9790ac; st_si=46619935239290; emshistory=%5B%22%E6%98%93%E8%A7%81%E8%82%A1%E4%BB%BD%22%2C%22%E6%98%93%E8%A7%81%E8%82%A1%E4%BB%BDg%E5%87%A0%E5%8F%A5%22%2C%22%E6%98%93%E8%A7%81%E8%82%A1%E4%BB%BDg%22%5D; st_asi=delete; st_pvi=23395488707365; st_sp=2022-12-05%2015%3A40%3A21; st_inirUrl=https%3A%2F%2Fwww.baidu.com%2Flink; st_sn=53; st_psi=20240301183231607-113104312931-0359581774',
            'Host:search-api-web.eastmoney.com',
            'Sec-Ch-Ua:"Google Chrome";v="113", "Chromium";v="113", "Not-A.Brand";v="24"',
            'Sec-Ch-Ua-Mobile:?0',
            'Sec-Ch-Ua-Platform:"Windows"',
            'Sec-Fetch-Dest:document',
            'Sec-Fetch-Mode:navigate',
            'Sec-Fetch-Site:none',
            'Sec-Fetch-User:?1',
            'Upgrade-Insecure-Requests:1',
            'User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36',
        ];
    }


    // 采集地址的参数
    // 东风财富网股吧
    //https://so.eastmoney.com/tiezi/s?keyword=易见股份

    //东风财富网资讯
    //https://so.eastmoney.com/news/s?keyword=易见股份

    //东风财富网文章
    //https://so.eastmoney.com/carticle/s?keyword=易见股份

    //东风财富网公告
    //https://so.eastmoney.com/ann/s?keyword=易见股份
    protected function makeApiParam($keyword, $type, $page = 1, $size = 10)
    {
        // 股吧
        if($type == 'tiezi'){
            return [
                "uid"           => "",
                "keyword"       => $keyword,
                "type"          => ["gubaArticleWeb"],
                "client"        => "web",
                "clientVersion" => "curr",
                "clientType"    => "web",
                "param"         => ["gubaArticleWeb" => ["pageSize" => $size, "pageIndex" => $page, "postTag" => "", "preTag" => "", "sortOrder" => ""]],
            ];
        }

        // 资讯
        if($type == 'news'){
            return [
                "uid"           => "",
                "keyword"       => $keyword,
                "type"          => ["cmsArticleWebOld"],
                "client"        => "web",
                "clientType"    => "web",
                "clientVersion" => "curr",
                "param"         => ["cmsArticleWebOld" => ["searchScope" => "default", "sort" => "default", "pageIndex" => $page, "pageSize" => $size, "preTag" => "<em>", "postTag" => "</em>"]],
            ];
        }

        // 文章
        if($type == 'carticle'){
            return [
                "uid"           => "",
                "keyword"       => $keyword,
                "type"          => ["article"],
                "client"        => "web",
                "clientType"    => "web",
                "clientVersion" => "curr",
                "param"         => ["article" => ["searchScope" => "ALL", "sort" => "DEFAULT", "pageIndex" => $page, "pageSize" => $size, "preTag" => "", "postTag" => ""]],
            ];
        }

        // 公告
        if($type == 'ann'){
            return [
                "uid"           => "",
                "keyword"       => $keyword,
                "type"          => ["noticeWeb"],
                "client"        => "web",
                "clientVersion" => "curr",
                "clientType"    => "web",
                "param"         => ["noticeWeb" => ["preTag" => "<em class=\"red\">", "postTag" => "</em>", "pageSize" => $size, "pageIndex" => $page]],
            ];
        }

        return [];
    }

    // 从股吧网站采集数据  html页面采集
    //  TODO 以前采集股吧页面，暂时未使用
    public function guba($stockId, $uid, $time, $weIds = [])
    {
        // 获取列表
        try{
            //$url   = 'https://guba.eastmoney.com/list,300688.html';
            $url = 'https://guba.eastmoney.com/list,'.$stockId.'.html';
            echo '采集：'.$url.chr(10);
            $rule  = ['html' => ['', 'html'], 'title' => ['', 'text']];
            $lists = $this->getQLList($url, '#mainlist .table_list .tab_content .defaultlist', 'table tr', $rule);
        } catch(\Exception $e){
            return $this->return_false($e->getMessage());
        }

        if(empty($lists)){
            echo '   股吧：没有采集内容'.chr(10);

            return;
        }

        $domain = $this->domain;
        $today  = strtotime(date('Y-m-d'));
        $year   = date('Y');
        $_rule  = ['html' => ['', 'html'], 'text' => ['', 'text'], 'link' => ['a', 'href']];
        foreach($lists as $key => $item){
            $_ql   = QueryList::html($item['html']);
            $_list = $_ql->range('td')->rules($_rule)->queryData();
            if(count($_list) < 5){
                continue;
            }
            // 组合数据
            $_item = [
                'read_no' => $_list[0]['text'],
                'comment' => trim($_list[1]['text']),
                'title'   => trim($_list[2]['text']),
                'href'    => $_list[2]['link'],
                'author'  => trim($_list[3]['text']),
                'date'    => $year.'-'.$_list[4]['text'],
            ];

            // 执行采集
            $_title  = $_item['title'];
            $_href   = $_item['href'];
            $_date   = $_item['date'];
            $_author = $_item['author'];
            $_ctime  = strtotime($_date); // 时间
            // 只采集有评论的
            /*if($_item['comment'] < 1){
                $this->showMsg(' '.($key + 1).'：==> 文章：评论数过少，不采集：'.$_title);
                continue;
            }*/
            // 采集地址
            //采集链接
            if(strpos($_href, 'http') === false){
                if(strpos($_href, '//') === false){
                    $_href = $domain.$_href;
                } else{
                    $_href = 'https:'.$_href;
                }
            }

            // 判断链接是否已经采集
            if($is_file){
                $info = $this->isGatherFile($_href);
            } else{
                $info = $this->isGather($_href);
            }
            if($info){
                if(empty($info['sentiment'])){
                    $sentiment = $this->baiduNlp($info['text'] ?: $info['description']);
                    if($sentiment){
                        if($is_file){
                            $info['sentiment'] = $sentiment;
                            $this->updateFile($_href, $info);
                        } else{
                            $this->updateArticle($info['id'], ['sentiment' => $sentiment]);
                        }
                    }
                }

                $this->showMsg(' '.($key + 1).'：==> 已经采集文章：'.$_title);
                continue;
            }
            // 只采集今天的
            if($_ctime < $today){
                $this->showMsg(' '.($key + 1).'：==> 文章：不是今天的文章：'.$_title);
                continue;
            }

            // 如果没有标题
            if(empty($_title)){
                continue;
            }
            // 查询
            try{
                $_ql = QueryList::get($_href);
            } catch(\Exception $e){
                return $this->return_false($e->getMessage());
            }

            // 如果有 p 标签
            $this->showMsg(' '.($key + 1).'：==> 采集文章：'.$_title);
            $rule  = ['html' => ['', 'html'], 'text' => ['', 'text']];
            $list  = $_ql->range('#newscontent .newstext  p')->rules($rule)->queryData();
            $_text = '';
            $_desc = '';
            if($list){
                foreach($list as $_item){
                    // 这里不直接用 $_item['text']  避免里面有图片
                    $_rangeHtml = $this->removeHtml($_item['html']);
                    if(empty($_rangeHtml)){
                        continue;
                    }
                    if(empty($_desc) && (strpos($_rangeHtml, '记者') === false) && (strpos($_rangeHtml, '转自') === false) && (strpos($_rangeHtml, '|') === false) && (strpos($_rangeHtml, '来源') === false)){
                        $_desc = $_rangeHtml;
                    }
                    //$_desc = str_replace('创业黑马(300688.SZ)', '', $_desc);
                    $_desc = $this->replaceDescribe($_desc);
                    $_text .= '<p style="padding: 3px 0;">'.$_rangeHtml.'</p>';
                }
            } else{
                // 如果没有 p 标签
                // 这里不直接用 ->text()  避免里面有图片
                //$_desc = $_ql->find('#artibody')->text();
                $_html = $_ql->find('#newscontent .newstext ')->html();
                $_html = $this->removeHtml($_html);
                $_desc = $this->replaceDescribe($_html);
                //$_desc = str_replace('创业黑马(300688.SZ)', '', $_html);
                if($_html){
                    $_text .= '<p style="padding: 3px 0;">'.$_html.'</p>';
                }
            }

            // echo chr(10).chr(10).chr(10);
            // var_dump($_date);
            // var_dump($today);
            //var_dump($_html);
            $_row = ['uid' => $uid, 'link' => $_href, 'title' => $_title, 'description' => $_desc, 'text' => $_text, 'time' => $time, 'author' => $_author, 'is_show' => true, 'to_channel' => 'dark', 'from_site' => $domain];
            var_dump($_row);

            //$this->showMsg('      ==> 保存到数据库：'.$_title);
            //$this->saveArticle($_row);


            //$this->showMsg('      ==> 发送到微信：'.var_export($weIds, 1));
            //$this->toWechatGroup($_title, $_href, $weIds);
        }

        return true;
    }


}

